package cn.retalsystem.controller;

import cn.retalsystem.bean.Admin;
import cn.retalsystem.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @Author: 官宝星
 * @Date: 2019/10/31 3:10
 * @Version 1.0
 * @描述：
 */
@Controller
@RequestMapping("/admin")
public class AdminController {
    @Autowired
    AdminService adminService;

    /**
     * 管理员登陆，将用户数据存入session，返回0或1
     * @param admin
     * @param session
     * @return
     */
    @RequestMapping("/login")
    @ResponseBody
    public int login(@RequestBody Admin admin, HttpSession session){
        Admin a = adminService.selectAdminByNamePassword(admin);
        if(a!=null){
            session.setAttribute("admin",a);
            return 1;
        }
        return 0;
    }

    /**
     * 前端从session中获取管理员信息
     * @param session
     * @return
     */
    @RequestMapping("/getAdmin")
    @ResponseBody
    public Admin getAdmin(HttpSession session){
        Admin admin = (Admin) session.getAttribute("admin");
        return admin;
    }

    /**
     * 管理员退出登陆
     * @param session
     * @return
     */
    @RequestMapping("/exit")
    @ResponseBody
    public boolean exit(HttpSession session){
        session.removeAttribute("admin");
        return true;
    }
}
